@using BlazorApp.Pages.Common.Html.HTable
@using BlazorApp.Pages.Common.Metadata
@using k8s.Models
@inherits BlazorApp.Pages.Common.PageBase

@if (Hpa != null)
{
    @if (SimpleView)
    {
        @foreach (var metric in Hpa.Spec.Metrics)
        {
            @if (metric.Pods != null)
            {
                <div>
                    <HPAMetricsDetailView Metric="@metric" CurrentMetrics="@CurrentMetrics" SimpleView="@SimpleView"></HPAMetricsDetailView>
                </div>
            }

            @if (metric.Resource != null)
            {
                <div>
                    <HPAMetricsDetailView Metric="@metric" CurrentMetrics="@CurrentMetrics" SimpleView="@SimpleView"></HPAMetricsDetailView>
                </div>
            }

            @if (metric.ObjectProperty != null)
            {
                <div>
                    <HPAMetricsDetailView Metric="@metric" Namespace="@Hpa.Namespace()" CurrentMetrics="@CurrentMetrics" SimpleView="@SimpleView"></HPAMetricsDetailView>
                </div>
            }

            @if (metric.External != null)
            {
                <div>
                    <HPAMetricsDetailView Metric="@metric" CurrentMetrics="@CurrentMetrics" SimpleView="@SimpleView"></HPAMetricsDetailView>
                </div>
            }

            @if (metric.ContainerResource != null)
            {
                <div>
                    <HPAMetricsDetailView Metric="@metric" CurrentMetrics="@CurrentMetrics" SimpleView="@SimpleView"></HPAMetricsDetailView>
                </div>
            }
        }
    }
    else
    {
        <Divider Orientation="left" Style="font-weight:bold">Metrics</Divider>
        @if (Hpa.Spec.Metrics is { Count: > 0 })
        {
            @foreach (var metric in Hpa.Spec.Metrics)
            {
                <HTable Bordered Column="1">
                    <PropertyOptionView Title=@L["Type"] ExplainField="horizontalPodAutoscaler.spec.metrics.type" Item="@metric.Type" Options='@(new[] { "ContainerResource", "External", "Object", "Pods", "Resource" })'></PropertyOptionView>

                    @if (metric.Pods != null)
                    {
                        <PropertySimpleView Title=@L["Pods"] ExplainField="horizontalPodAutoscaler.spec.metrics.pods" Item="@metric.Pods" ShowInJson="true">
                            <HPAMetricsDetailView Metric="@metric" CurrentMetrics="@CurrentMetrics"></HPAMetricsDetailView>
                        </PropertySimpleView>
                    }
                    @if (metric.Resource != null)
                    {
                        <PropertySimpleView Title=@L["Resource"] ExplainField="horizontalPodAutoscaler.spec.metrics.resource" Item="@metric.Resource" ShowInJson="true">
                            <HPAMetricsDetailView Metric="@metric" CurrentMetrics="@CurrentMetrics"></HPAMetricsDetailView>
                        </PropertySimpleView>
                    }
                    @if (metric.ObjectProperty != null)
                    {
                        <PropertySimpleView Title=@L["Object"] ExplainField="horizontalPodAutoscaler.spec.metrics.object"
                                            Item="@metric.ObjectProperty" ShowInJson="true">
                            <HPAMetricsDetailView Metric="@metric" Namespace="@Hpa.Namespace()" CurrentMetrics="@CurrentMetrics"></HPAMetricsDetailView>
                        </PropertySimpleView>
                    }
                    @if (metric.External != null)
                    {
                        <PropertySimpleView Title=@L["External"]
                                            ExplainField="horizontalPodAutoscaler.spec.metrics.external"
                                            Item="@metric.External" ShowInJson="true">
                            <HPAMetricsDetailView Metric="@metric" CurrentMetrics="@CurrentMetrics"></HPAMetricsDetailView>
                        </PropertySimpleView>
                    }
                    @if (metric.ContainerResource != null)
                    {
                        <PropertySimpleView Title=@L["ContainerResource"]
                                            ExplainField="horizontalPodAutoscaler.spec.metrics.containerResource"
                                            Item="@metric.ContainerResource" ShowInJson="true">
                            <HPAMetricsDetailView Metric="@metric" CurrentMetrics="@CurrentMetrics"></HPAMetricsDetailView>
                        </PropertySimpleView>
                    }
                </HTable>
            }
        }
    }
}


@code {

    [Parameter] public V2HorizontalPodAutoscaler Hpa { get; set; }

    [Parameter] public bool SimpleView { get; set; } = false;

    private IList<V2MetricStatus> CurrentMetrics { get; set; }

    protected override async Task OnInitializedAsync()
    {
        CurrentMetrics = Hpa.Status.CurrentMetrics;
        // FOR TEST
        // CurrentMetrics = new List<V2MetricStatus>()
        // {
        //     new V2MetricStatus()
        //     {
        //         Pods = new V2PodsMetricStatus()
        //         {
        //             Current = new V2MetricValueStatus()
        //             {
        //                 AverageValue = new ResourceQuantity("100k")
        //             },
        //             Metric = new V2MetricIdentifier()
        //             {
        //                 Name = "packets-per-second"
        //             }
        //         }
        //     },
        //     new V2MetricStatus()
        //     {
        //         External = new V2ExternalMetricStatus()
        //         {
        //             Current = new V2MetricValueStatus()
        //             {
        //                 AverageValue = new ResourceQuantity("30")
        //             },
        //             Metric = new V2MetricIdentifier()
        //             {
        //                 Name = "queue_messages_ready",
        //             },
        //         }
        //     },
        //     new V2MetricStatus()
        //     {
        //         Resource = new V2ResourceMetricStatus()
        //         {
        //             Current = new V2MetricValueStatus()
        //             {
        //                 AverageValue       = new ResourceQuantity("30"),
        //                 Value              = new ResourceQuantity("30"),
        //                 AverageUtilization = 30
        //             },
        //             Name = "cpu"
        //         }
        //     },
        //     new V2MetricStatus()
        //     {
        //         ObjectProperty = new V2ObjectMetricStatus()
        //         {
        //             Current = new V2MetricValueStatus()
        //             {
        //                 Value = new ResourceQuantity("11k")
        //             },
        //             Metric = new V2MetricIdentifier()
        //             {
        //                 Name = "requests-per-second"
        //             }
        //         }
        //     },
        //     new V2MetricStatus()
        //     {
        //         ContainerResource = new V2ContainerResourceMetricStatus()
        //         {
        //             Current = new V2MetricValueStatus()
        //             {
        //                 AverageValue       = new ResourceQuantity("40"),
        //                 AverageUtilization = 40,
        //             },
        //             Name = "cpu"
        //         }
        //     }
        // };
        await base.OnInitializedAsync();
    }

}
